package com.zhdl.modules.assemblybolt;

import com.google.common.eventbus.Subscribe;
import com.project.common.util.DelayUtil;
import com.zhdl.common.eventbus.center.EventBusCenter;
import com.zhdl.modules.assemblybolt.web.req.AssemblyBoltData;
import com.zhdl.modules.assemblybolt.web.req.CarryingPalletsData;
import com.zhdl.modules.assemblybolt.service.AssemblyBoltService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Slf4j
@Service
public class CarryingPalletsProcess {

    /**
     * 当前流程是否在运行 - 默认未运行
     * 当前流程是否在测试 - 默认未测试
     * true 运行 false 未运行
     */
    public boolean isRunning = false, isTest = false;
    /**
     * 电机运动流程
     */
    public int cpMotorProcess = -1;
    @Resource
    public ThreadPoolTaskScheduler taskScheduler1;
    /**
     * 西门子Plc 寄存器值
     */
    String siemensValue0To404;
    CarryingPalletsData carryingPalletsData = new CarryingPalletsData();
    @Resource
    private AssemblyBoltService assemblyBoltService;

    @Subscribe
    public void dataReception(AssemblyBoltData data) {
        siemensValue0To404 = data.getSiemensValue0To404();
        log.info("siemensValue0To404：" + siemensValue0To404);
    }

    public void release() {
        isRunning = false;
    }

    /**
     * 初始化
     */
    public void init() {
        EventBusCenter.getEventBus().register(this);
        release();
    }

    /**
     * 流程开始
     */
    public void ProcessStartCP() {
        /*
            判断当前流程是否正在运行
         */
        if (isRunning) {
            return;
        }
        isRunning = true;

        log.info("------------------流程开始运动------------------");

        /*
         * 判断 转载托盘是否在待机位
         */
        assemblyBoltService.readMotorIOStatus(6, aBoolean -> {
            if (!aBoolean) {
                motorAxis1Back();
                assemblyBoltService.readPlcMotorRunDone(1, aBoolean1 -> {
                    if (aBoolean1) {
                        assemblyBoltService.readMotorIOStatus(6, aBoolean11 -> {
                            if (aBoolean11) {
                                motorAxis1To3();
                            }
                        });
                    }
                });
            } else {
                motorAxis1To3();
            }
        });
    }

    public void motorAxis1To3() {
        cpMotorProcess++;
        switch (cpMotorProcess) {
            case 1 -> {
                motorAxis1Run();
                assemblyBoltService.readPlcMotorRunDone(1, aBoolean -> {
                    if (aBoolean) {
                        assemblyBoltService.readMotorIOStatus(5, aBoolean1 -> {
                            if (aBoolean1) {
                                log.info("轴1-运动至加工位");
                                motorAxis1To3();
                            }
                        });
                    }
                });
            }
            case 2 -> {
                motorAxis2Run();
                assemblyBoltService.readPlcMotorRunDone(2, aBoolean -> {
                    motorAxis1To3();
                    log.info("夹紧完成");
                });
            }
            case 3 -> {
                final boolean[] takePhoto = {true};
                log.info("相机拍照，是否在原点");
                taskScheduler1.scheduleWithFixedDelay(() -> {
                    if (takePhoto[0]) {
                        log.info("转运流程完成");
                    } else {
                        motorAxis3AntiClockwise(carryingPalletsData.getAxis3CDistance());
                        log.info("相机拍照，是否在原点");
                        takePhoto[0] = true;
                    }
                }, 100);
            }
        }
    }

    public void motorAxis3To1() {
        cpMotorProcess++;
        switch (cpMotorProcess) {
            case 1 -> {
                motorAxis1Back();
                assemblyBoltService.readPlcMotorRunDone(1, aBoolean -> {
                    if (aBoolean) {
                        assemblyBoltService.readMotorIOStatus(6, aBoolean1 -> {
                            if (aBoolean1) {
                                log.info("轴1-运动至等待位");
                                motorAxis3To1();
                            }
                        });
                    }
                });
            }
            case 2 -> {
                motorAxis2Back();
                assemblyBoltService.readPlcMotorRunDone(2, aBoolean -> {
                    motorAxis3To1();
                    log.info("夹紧完成");
                });
            }
        }
    }

    /**
     * 轴一 移动托盘至加工位
     */
    public void motorAxis1Run() {
        assemblyBoltService.plcControlMotorRunX(1, 200, carryingPalletsData.getAxis1YDistance());
        DelayUtil.delayedExecution(100, () -> {
            assemblyBoltService.plcControlMotorAbsRun(1, true);
            log.info("电机前推完成-轴一");
        });
    }

    /**
     * 轴一 回到原点 等待放料
     */
    public void motorAxis1Back() {
        assemblyBoltService.plcControlMotorRunX(1, 200, 0);
        DelayUtil.delayedExecution(100, () -> {
            assemblyBoltService.plcControlMotorAbsRun(1, true);
            log.info("电机前推完成-轴一");
        });
    }

    /**
     * 轴二 托盘夹紧
     */
    public void motorAxis2Run() {
        assemblyBoltService.plcControlMotorRunX(2, 200, carryingPalletsData.getAxis2ODistance());
        DelayUtil.delayedExecution(100, () -> {
            assemblyBoltService.plcControlMotorAbsRun(2, true);
            log.info("电机前推完成-轴一");
        });
    }

    /**
     * 轴二 托盘松开
     */
    public void motorAxis2Back() {
        assemblyBoltService.plcControlMotorRunX(2, 200, 0);
        DelayUtil.delayedExecution(100, () -> {
            assemblyBoltService.plcControlMotorAbsRun(2, true);
            log.info("电机前推完成-轴一");
        });
    }

    /**
     * 轴二 托盘旋转 逆时针
     */
    public void motorAxis3AntiClockwise(int distance) {
        assemblyBoltService.plcControlMotorRunX(3, 200, distance);
        DelayUtil.delayedExecution(100, () -> {
            assemblyBoltService.plcControlMotorAbsRun(3, true);
            log.info("电机前推完成-轴一");
        });
    }

    /**
     * 轴三 托盘旋转 顺时针
     */
    public void motorAxis3Clockwise(int distance) {
        assemblyBoltService.plcControlMotorRunX(3, 200, distance);
        DelayUtil.delayedExecution(100, () -> {
            assemblyBoltService.plcControlMotorAbsRun(3, true);
            log.info("电机前推完成-轴一");
        });
    }

}
